#include <iostream>
#include <stdexcept>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
#include "Level.hpp"
#include "PhysicWorld.hpp"
#include "Camera.hpp"

int main(int argc, char **argv)
{
    if(argc <= 1)
    {
        std::cerr << "No level given." << std::endl;
        return EXIT_FAILURE;
    }
    PhysicWorld::instance().createWorld();
    Level l;
    try
    {
        l.loadFromFile(argv[1]);
    }
    catch(std::exception& e)
    {
        std::cerr << "Error while loading level : " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    
    sf::RenderWindow window(sf::VideoMode(640,480,32),"FunBall !");
    window.setView(GET_CAMERA);
    window.setFramerateLimit(60);
    Game game(l);
    while(window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
            window.close();
    
        game.update(1.f/60.f);
        window.setView(GET_CAMERA);        
        window.clear(sf::Color::White);
        game.draw(window);
        window.display();
    }
    
    return EXIT_SUCCESS;
}   


